#!/bin/sh /etc/rc.common
# Copyight (C) 2017  www.17ce.com
START=99
CDN_BASE="http://git.oschina.net/mjyhj/K2_17ce/raw/master/"
UPDATE_URL="http://git.oschina.net/mjyhj/K2_17ce/raw/master/17ce_version.php"
WORK_DIR="/tmp/17ce"
UPDATE_FILE="$WORK_DIR/update.tgz"
TEMP_FILE="$WORK_DIR/update.txt"
SAVE_DIR="/usr/share/17ce"
UUID="$SAVE_DIR/UUID"
USER="$SAVE_DIR/user"
CDN_BASE="http://git.oschina.net/mjyhj/K2_17ce/raw/master/"
GET_CMD="wget -T 60 -O "
wget_install(){
	wget -T 60 -O $1  $2
	chmod +x $1
}
check_update()
{
	wget_install  $TEMP_FILE   $UPDATE_URL
        TURL=`cat $TEMP_FILE|awk '{print $2}'`
        echo "will download update file -> $TURL"
        wget -T 60 $TURL  -O $UPDATE_FILE
	wget_install  $UPDATE_FILE   $TURL
	tar xzvf $UPDATE_FILE -C $WORK_DIR 
}
init_conf()
{
        mkdir -p $WORK_DIR
        mkdir -p $SAVE_DIR
        rm -rf $TEMP_FILE  $UPDATE_FILE
	check_update
	wget_install $WORK_DIR/libstdc++.so.6   $CDN_BASE/lib/libstdc++.so.6.0.21
        wget_install $WORK_DIR/libpolarssl.so.7   $CDN_BASE/lib/libpolarssl.so.1.3.9
        wget_install $WORK_DIR/libcurl.so.4   $CDN_BASE/lib/libcurl.so.4
        echo "create link"
        ln -sf /tmp/17ce/libpolarssl.so.7  /usr/lib/libpolarssl.so.7
        ln -sf /tmp/17ce/libcurl.so.4  /usr/lib/libcurl.so.4
        ln -sf /lib/libc.so.0  /tmp/17ce/libc.so
        ln -sf /usr/lib/libpolarssl.so  /tmp/17ce/libmbedtls.so.9
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$WORK_DIR
}

start()
{
        echo "begin start 17ce"
        echo "~~~~~~"
        sleep 2
        echo "~~~~~~"
        sleep 2
        echo "~~~~~~"
        sleep 2
        init_conf
        $WORK_DIR/17ce_v3 -u 171285200@qq.com
        echo "17ce Client has stated."
        save_file UUID
        save_file user
}

stop()
{
        killall -9 17ce_v3
        sleep 1
        echo "17ce Client has stoped."
}
